class Proxy {
    constructor(result) {
        this.result = result;
        this.isProxy = '';
        this.proxyType = '';
        this.proxyTime = '';
        this.parseProxy();
    }

    parseProxy() {
        let infos = this.result.split('|');
        if (infos.length !== 3) {
            console.log('查询结果格式错误或离线库非代理v2版本库');
            return;
        }
        this.isProxy = infos[0];
        let proxyTypes = infos[1].split(',');
        let p = [];
        
        for (let i = 0; i < proxyTypes.length; i++) {
            if (proxyTypes[i].trim() !== '') {
                p.push(proxyTypes[i]);
            }
        }

        this.proxyType = p.join('|');
        this.proxyTime = infos[2];
    }

    getProxyType() {
        return this.proxyType;
    }

    getProxyTime() {
        return this.proxyTime;
    }

    getIsProxy(){
        return this.isProxy;
    }
}


class Scenes {
    constructor(result) {
        this.result = result;
        this.scenes = null;
        this.parseScene();
    }

    parseScene() {
        const infos = this.result.split("|");
        if (infos.length !== 1) {
            throw new Error("查询结果格式错误或离线库非场景库");
        }
        const scenesCn = (() => {
            switch (infos[0]) {
                case "COM": return "商业公司";
                case "EDU": return "教育机构";
                case "GTW": return "企业专线";
                case "GOV": return "政府机构";
                case "NET": return "基础设施";
                case "ORG": return "组织机构";
                case "DYN": return "家庭宽带";
                case "IDC": return "数据中心";
                case "MOB": return "移动网络";
                case "CDN": return "内容分发";
                case "DNS": return "域名解析";
                case "BOGON": return "保留";
                default: return "-";  // 处理未匹配的情况
            }
        })();
        this.scenes = scenesCn;
    }
}


class Location {
    constructor(result) {
        this.result = result;
        this.continent = null; // 洲
        this.country = null; // 国家/地区
        this.province = null; // 省份
        this.city = null; // 城市
        this.district = null; // 区县
        this.isp = null; // 运营商
        this.areaCode = null; // 区域代码
        this.countryEnglish = null; // 国家/地区英文
        this.countryCode = null; // 国家/地区英文简写
        this.longitude = null; // 经度
        this.latitude = null; // 纬度
        this.elevation = null; // 海拔
        this.zipCode = null; // 邮编
        this.cityCode = null; // 城市代码
        this.weatherStation = null; // 气象站
        this.asn = null;
        this.preserve = null; // 预留
        this.street = null; // 街道
        this.timeZone = null; // 时区

        this.parseLocation();
    }

    parseLocation() {
        const infos = this.result.split("|");
        if (infos.length !== 19) {
            throw new Error("查询结果格式错误或离线库非归属地库");
        }
        this.continent = infos[0];
        this.country = infos[1];
        this.province = infos[2];
        this.city = infos[3];
        this.district = infos[4];
        this.isp = infos[5];
        this.areaCode = infos[6];
        this.countryEnglish = infos[7];
        this.countryCode = infos[8];
        this.longitude = infos[9];
        this.latitude = infos[10];
        this.elevation = infos[11];
        this.zipCode = infos[12];
        this.cityCode = infos[13];
        this.weatherStation = infos[14];
        this.asn = infos[15];
        this.preserve = infos[16];
        this.street = infos[17];
        this.timeZone = infos[18];
    }

    toString() {
        return `Location{
            continent='${this.continent}',
            country='${this.country}',
            province='${this.province}',
            city='${this.city}',
            district='${this.district}',
            isp='${this.isp}',
            areaCode='${this.areaCode}',
            countryEnglish='${this.countryEnglish}',
            countryCode='${this.countryCode}',
            longitude='${this.longitude}',
            latitude='${this.latitude}',
            elevation='${this.elevation}',
            zipCode='${this.zipCode}',
            cityCode='${this.cityCode}',
            weatherStation='${this.weatherStation}',
            asn='${this.asn}',
            preserve='${this.preserve}',
            street='${this.street}',
            timeZone='${this.timeZone}'
        }`;
    }
}

module.exports = {
    Proxy,
    Scenes,
    Location
};